package com.atguigu.demo.controller;

import com.atguigu.demo.entity.Result;
import com.atguigu.demo.entity.Tiger;
import com.atguigu.demo.service.api.TigerService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class TigerController {

    @Autowired
    private TigerService tigerService;

    @GetMapping("/tiger/list")
    public Result<List<Tiger>> getTigerList() {

        List<Tiger> tigerList = tigerService.getTigerList();

        return Result.ok(tigerList);
    }

    // /tiger/5
    @DeleteMapping("/tiger/{tigerId}")
    public Result<Void> removeTigerById(@PathVariable("tigerId") Integer tigerId) {

        tigerService.removeTigerById(tigerId);

        return Result.ok();
    }

    @PostMapping("/tiger")
    public Result<Void> saveTiger(@RequestBody Tiger tiger) {

        tigerService.saveTiger(tiger);

        return Result.ok();
    }

    @GetMapping("/tiger/{tigerId}")
    public Result<Tiger> getTigerById(@PathVariable("tigerId") Integer tigerId) {

        Tiger tiger = tigerService.getTigerById(tigerId);

        return Result.ok(tiger);
    }

    @PutMapping("/tiger")
    public Result<Void> updateTiger(@RequestBody Tiger tiger) {

        tigerService.updateTiger(tiger);

        return Result.ok();
    }

    @GetMapping("/tiger/page/{pageNum}/{pageSize}")
    public Result<PageInfo<Tiger>> getTigerPage(
            @PathVariable Integer pageNum,
            @PathVariable Integer pageSize,
            @RequestParam(value = "tigerName", defaultValue = "") String tigerName) {

        PageInfo<Tiger> pageInfo = tigerService.getTigerPage(pageNum, pageSize, tigerName);

        return Result.ok(pageInfo);
    }
}
